package Identify_Exception;

import java.util.Scanner;
//20230404430920沈泽华
public class Exception {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int age = 0;
        String phone_number = "0";

        System.out.println("请输入学生姓名");
        String name = sc.next();
        //年龄age异常捕获部分
        while(true){
            try {
                System.out.println("请输入学生年龄：");
                age = sc.nextInt();
                if(age < 0 || age > 120)
                    throw new ageException("年龄范围有误，请输入0~120内的数字");
                else
                    break;
            }catch(ageException e){
                System.out.println(e.getMessage());
                sc.nextLine();//防止数据不停输入
            }
            catch(java.lang.Exception e){
                System.out.println("年龄输入不符合规范");
                sc.nextLine();//防止数据不停输入
            }
        }
        //手机号码异常部分
        boolean bl = true;
        while(bl){
            System.out.println("请输入学生手机号码：");
            phone_number = sc.next();
        try {
            if (phone_number.length() != 11) {
                throw new phonenumException("不是11位手机号码，请输入11位！");
            }
        }
            catch(phonenumException e){
                System.out.println(e.getMessage());
                sc.nextLine();
                continue;
            }
            //用阿斯克码进行字符串的纯数字判断
            for (int i = 0; i < phone_number.length(); ++i) {
                int Char = phone_number.charAt(i);
                if (Char < 48 || Char > 57) {
                    bl = true;
                    break;
                } else
                    bl = false;
            }

            if (bl)
                System.out.println("不是纯数字！请输入11位纯数字");
            else
                break;
            sc.nextLine();
        }

        Student student = new Student(name,age,phone_number);
        student.showInfo();
    }
}
//定义学生类Student
//属性：姓名name，年龄age，手机号码phone_number
//条件：age > 0 && age < 120,phone_number 11 位
class Student{
    String name;
    int age;
    String phone_number;
    public Student(String name,int age,String phone_number){
        this.name = name;
        this.age = age;
        this.phone_number = phone_number;
    }

    public void showInfo(){
        System.out.println("学生姓名："+this.name+" 年龄："+this.age+" 手机号码："+this.phone_number);
    }
}

//定义异常
class ageException extends java.lang.Exception {
    public ageException(String message){
        super(message);
    }
}

class phonenumException extends java.lang.Exception{
    public phonenumException(String message){
        super(message);
    }
}